
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vo.VeiculoVO;

public class VeiculoDAO {
    
    public static List<VeiculoVO> getVeiculos() {

        Connection con = ConnectionFactory.getConnection();

        List<VeiculoVO> lista = new ArrayList<>();

        try {

            String query = "SELECT * FROM Veiculo";
            PreparedStatement stmt = con.prepareStatement(query);
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
               
                String placa = res.getString(1);
                String marca = res.getString(2);
                String modelo = res.getString(3);
                String ano = res.getString(4);
                int kilometragem = res.getInt(5);
                String categoria = res.getString(6);
                boolean Status = res.getBoolean(7);
                String fotoPath = res.getString(8);
               
                lista.add(new VeiculoVO(placa,marca,modelo,ano,kilometragem,categoria,Status,fotoPath));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
    public static boolean VeiculoExiste(String Placa){
        Connection con= ConnectionFactory.getConnection();
        String novaPlaca;
        
        try{
            String query = "Select * from Veiculo where placa= ?";
            PreparedStatement stmt=con.prepareStatement(query);
            stmt.setString(1,Placa);
             ResultSet res = stmt.executeQuery();
            while(res.next()){
            novaPlaca = res.getString(1);
            
            if(novaPlaca.equals(Placa))
                return true;
            }
        }catch(Exception e){
            
        }
        return false;
    }
    public static List<VeiculoVO> getVeiculoPelaPlaca(String ParamPlaca){

        Connection con = ConnectionFactory.getConnection();
        
        List<VeiculoVO> lista =new ArrayList<>();
        
        try{
            String query = "SELECT * FROM Veiculo Where Placa = ? ";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1,ParamPlaca);
            ResultSet res = stmt.executeQuery();
            
            while (res.next()){
                String placa = res.getString(1);
                String marca = res.getString(2);
                String Modelo = res.getString(3);
                String Ano = res.getString(4);
                int Kilometragem = res.getInt(5);
                String Categoria = res.getString(6);
                boolean Status = res.getBoolean(7);
                String Foto = res.getString(8);
                
                lista.add(new VeiculoVO(placa,marca,Modelo,Ano,Kilometragem,Categoria,Status,Foto));
                
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return lista;
        
    }
    
    
   public static void alterarVeiculo(String Placa,String KM,String Foto)
   {
       Connection con=ConnectionFactory.getConnection();
       
       try
       {
           
           String query="Update Veiculo set ULTIMA_KILOMETRAGEM=?, FOTO_PATH=? where placa=?";
           PreparedStatement stmt = con.prepareStatement(query);
           stmt.setString(1,KM);
           stmt.setString(2,Foto);
           stmt.setString(3,Placa);
           
           stmt.executeUpdate();
           
           
       }catch(Exception e){
           e.printStackTrace();
           
       }
   }
   public static void deleteVeiculoPela(String placa){
        Connection con=ConnectionFactory.getConnection();
        try{
            String query="DELETE from Veiculo Where placa='"+placa+"'";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.executeUpdate();
        }catch(Exception e){e.printStackTrace();}
    }
   public static void insertVeiculo(VeiculoVO veiculo){
    
    Connection con = ConnectionFactory.getConnection();
        
        try {

            String query = "INSERT INTO Veiculo VALUES(?,?,?,?,?,?,?,?)";
            PreparedStatement stmt = con.prepareStatement(query);
            
            stmt.setString(1,veiculo.getPlaca());
            stmt.setString(2, veiculo.getMarca());
            stmt.setString(3,veiculo.getModelo());
            stmt.setString(4,veiculo.getAno());
            stmt.setInt(5,veiculo.getKilometragem());
            stmt.setString(6, veiculo.getCategoria());
            stmt.setBoolean(7,veiculo.getStatus());
            stmt.setString(8, veiculo.getFotoPath());
            stmt.executeUpdate();

           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void alteraStatus(String Placa) {
        Connection con = ConnectionFactory.getConnection();
        
      if(getStatusPelaPlaca(Placa)==true)
      {
       
       try
       {
           String query="Update Veiculo set status=?, where placa=?";
           PreparedStatement stmt = con.prepareStatement(query);
           stmt.setBoolean(1,false);
           stmt.setString(2,Placa);
           stmt.executeUpdate();     
       }catch(Exception e){
           e.printStackTrace(); 
       }         
      }
      else
      {     
       try
       {
           String query="Update Veiculo set status=? where placa=?";
           PreparedStatement stmt = con.prepareStatement(query);
           stmt.setBoolean(1,true);
           stmt.setString(2,Placa);        
           stmt.executeUpdate();

       }catch(Exception e){
           e.printStackTrace();          
       }         
      }
   
    }

    public static boolean getStatusPelaPlaca(String Placa) {

        Connection con = ConnectionFactory.getConnection();
        

        try{
            String query = "SELECT * FROM Veiculo Where Placa = ? ";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1,Placa);
            ResultSet res = stmt.executeQuery();
            
            while (res.next()){

                boolean Status = res.getBoolean(7);
 
                
                return Status;
                
            }
        }catch (Exception e){
            e.printStackTrace();
        }
       return false;
    }
        
}
